package com.shuoyue.ycgk.ui.recruitment;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.shuoyue.ycgk.Constant;
import com.shuoyue.ycgk.R;
import com.shuoyue.ycgk.appcatch.SPUtils;
import com.shuoyue.ycgk.base.BaseMvpFragment;
import com.shuoyue.ycgk.entity.RecruitmentItem;
import com.shuoyue.ycgk.entity.RecruitmentMainInfo;
import com.shuoyue.ycgk.entity.Type;
import com.shuoyue.ycgk.event.SwitchBaseEvent;
import com.shuoyue.ycgk.ui.common.CommonModuleContract;
import com.shuoyue.ycgk.ui.common.CstWebActivity;
import com.shuoyue.ycgk.ui.course.CourseTypeActivity;
import com.shuoyue.ycgk.ui.recruitment.focuse.MyFocuseActivity;
import com.shuoyue.ycgk.ui.recruitment.his.MyHisActivity;
import com.shuoyue.ycgk.ui.recruitment.job.JobListActivity;
import com.shuoyue.ycgk.ui.recruitment.newsinfo.NewNoticeActivity;
import com.shuoyue.ycgk.ui.recruitment.newsinfo.NoticeAdapter;
import com.shuoyue.ycgk.ui.recruitment.newsinfo.RecurimentInfoActivity;
import com.shuoyue.ycgk.ui.recruitment.resume.MyResumeActivity;
import com.shuoyue.ycgk.ui.search.SearchActivity;
import com.shuoyue.ycgk.ui.shop.ShopTabActivity;
import com.shuoyue.ycgk.utils.SizeUtil;
import com.shuoyue.ycgk.utils.SpacesItemDecoration;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class FragmentNotice extends BaseMvpFragment<NoticeMainContract.Presenter>
        implements NoticeMainContract.View,
        CommonModuleContract.View {

    @BindView(R.id.search_edit)
    TextView searchEdit;
    @BindView(R.id.service)
    ImageView service;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.weeknoticeNum)
    TextView weeknoticeNum;
    @BindView(R.id.weekJobNum)
    TextView weekJobNum;
    @BindView(R.id.edit_resume)
    TextView editResume;

    CommonModuleContract.Presenter modelPresenter;
    NoticeMainAdapter noticeAdapter;
    List<Type> baseTypes;
    Type type;
    @BindView(R.id.refresh_layout)
    SmartRefreshLayout refreshlayout;

    public static FragmentNotice getInstance() {
        FragmentNotice fragment = new FragmentNotice();
        return fragment;
    }

    @Override
    public void initParams(Bundle bundle) {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_notice;
    }

    @Override
    protected void initView() {
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.addItemDecoration(new SpacesItemDecoration(SizeUtil.dip2px(mContext, 8), false));
        noticeAdapter = new NoticeMainAdapter(null);
        recyclerView.setAdapter(noticeAdapter);
        noticeAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            RecruitmentItem recruitmentItem = (RecruitmentItem) adapter.getItem(position);
            startActivity(new Intent(mContext, RecurimentInfoActivity.class)
                    .putExtra("id", recruitmentItem.getId())
            );
        });
        refreshlayout.setEnableLoadMore(false);
        refreshlayout.setOnRefreshListener(refreshlayout -> {
            if (type != null) {
                mPresenter.getNoticeMain(type.getId());
            } else {
                modelPresenter.getBaseModels(4);
            }
            refreshlayout.finishRefresh(2000);
        });
    }

    @Override
    protected void initData() {
        super.initData();
        mPresenter = new NoticeMainContract.Presenter();
        mPresenter.attachView(this);
        modelPresenter = new CommonModuleContract.Presenter();
        modelPresenter.attachView(this);
        modelPresenter.getBaseModels(4);
    }

    @OnClick({R.id.course, R.id.shop, R.id.service,
            R.id.more, R.id.lay_newnotice,
            R.id.lay_newjob, R.id.my_resume,
            R.id.my_focuse, R.id.my_scan,
            R.id.edit_resume, R.id.search_edit})
    public void onViewClicked(View view) {
        if (type == null) {
            return;
        }
        switch (view.getId()) {
            case R.id.course:
                startActivity(new Intent(mContext, CourseTypeActivity.class));
                break;
            case R.id.shop:
                startActivity(new Intent(mContext, ShopTabActivity.class));
                break;
            case R.id.service:
//                CstWebActivity.start(mContext, Constant.serviceUrl, "咨询");
//                startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(Constant.serviceUrl)));
                if(Constant.isOpenServiceExploer){
                    Uri uri = Uri.parse(Constant.serviceUrl);
                    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                    startActivity(intent);
                }else {
                    CstWebActivity.start(mContext, Constant.serviceUrl, "咨询");

                }

                break;
            case R.id.more:
                startActivity(new Intent(mContext, NewNoticeActivity.class)
                        .putExtra("modelId", type.getId())
                        .putExtra("title", "公告列表"));
                break;
            case R.id.lay_newnotice:
                startActivity(new Intent(mContext, NewNoticeActivity.class)
                        .putExtra("modelId", type.getId())
                        .putExtra("title", "新公告"));
                break;
            case R.id.lay_newjob:
                startActivity(new Intent(mContext, JobListActivity.class)
                        .putExtra("modelId", type.getId()));
                break;

            case R.id.my_focuse:
                startActivity(new Intent(mContext, MyFocuseActivity.class)
                        .putExtra("modelId", type.getId()));
                break;
            case R.id.my_scan:
                startActivity(new Intent(mContext, MyHisActivity.class)
                        .putExtra("modelId", type.getId()));
                break;
            case R.id.my_resume:
            case R.id.edit_resume:
                startActivity(new Intent(mContext, MyResumeActivity.class));
                break;
            case R.id.search_edit:
                SearchActivity.start(mActivity, 4, 0, "招考公告");
                break;
        }
    }

    @Override
    public void setNoticeMainInfo(RecruitmentMainInfo info) {
        weeknoticeNum.setText("一周新公告" + info.getNewExamination() + "个");
        weekJobNum.setText("一周新职位" + info.getNewPost() + "个");
        noticeAdapter.setNewData(info.getExaminationNoticeListDTOS());
        refreshlayout.finishRefresh();
        refreshlayout.finishLoadMore();
    }

    @Override
    public void setBaseType(List<Type> baseTypes) {
        this.baseTypes = baseTypes;
        String perName = SPUtils.getLastSelectMoudle(mContext);
        if (perName != null && (
                perName.equals("教师招聘")
                        || perName.equals("公务员")
                        || perName.equals("事业单位"))) {
            for (Type baseType : baseTypes) {
                if (perName.equals(baseType.getName())) {
                    this.type = baseType;
                }
            }
        } else {
            if (this.type == null)
                type = baseTypes.get(0);

        }

        mPresenter.getNoticeMain(type.getId());
    }

    @Override
    public void setTabType(List<Type> type) {

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void switchBaseTypeEvent(SwitchBaseEvent s) {
        if (type != null) {
            if ("教师招聘".equals(s.getName())
                    || "公务员".equals(s.getName())
                    || "事业单位".equals(s.getName())) {
                for (Type baseType : baseTypes) {
                    baseType.setSelect(false);
                }
                for (Type baseType : baseTypes) {
                    if (s.getName().equals(baseType.getName())) {
                        baseType.setSelect(true);
                        type = baseType;
                        mPresenter.getNoticeMain(baseType.getId());
                        break;
                    }
                }
            }
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        EventBus.getDefault().register(this);
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onDestroyView() {
        EventBus.getDefault().unregister(this);
        super.onDestroyView();
    }
}
